package util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.views.DocumentView;

import pojo.CayThaoLuanPOJO;
import pojo.NguoiDungPOJO;
import pojo.NhomPOJO;
import pojo.NodeThaoLuanPOJO;
import pojo.TinNhanPOJO;

import DataAccess.CayThaoLuanDA;
import DataAccess.NguoiDungDA;
import DataAccess.NhomDA;
import DataAccess.NodeThaoLuanDA;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class XMLBuidler {
	public static Document BuildXML(String root) {
		Document doc = null;
		DocumentBuilder builder = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		try {
			builder = factory.newDocumentBuilder();
			doc = builder.newDocument();
			Element _root = doc.createElement(root);        
			doc.appendChild(_root);
			// Normalizing the DOM
			doc.getDocumentElement().normalize();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		return doc;
	}
	private static Document CreateOrderDOM(String root, Document doc)
	{
		DocumentBuilder builder = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try
		{
			builder = factory.newDocumentBuilder();
			doc = builder.newDocument();

		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}                
		Element _root = doc.createElement(root);        
		doc.appendChild(_root);
		// Normalizing the DOM
		doc.getDocumentElement().normalize();
		return doc;
	}

	public static String getContact(String tenDangNhap) {
		Document doc = BuildXML("Contact");		
		Element root = (Element) doc.getFirstChild();

		// Đọc từ database lên
		NhomDA nhomDA = new NhomDA();
		NguoiDungDA nguoiDungDA = new NguoiDungDA();
		ArrayList<NhomPOJO> lstNhom = nhomDA.getNhomNguoiDung(tenDangNhap);
		if (lstNhom != null) {
			for (int i = 0; i < lstNhom.size(); i++) {
				Element eNhom = lstNhom.get(i).createContactXMLElement(doc);
				ArrayList<NguoiDungPOJO> lstThanhVien = nguoiDungDA.getNguoiDungCuaNhom(lstNhom.get(i).getId());

				if (lstThanhVien != null) {
					for (int j = 0; j < lstThanhVien.size(); j++) {
						Element eNguoiDung = lstThanhVien.get(j).createContactXMLElement(doc);
						eNhom.appendChild(eNguoiDung);
					}
				}
				root.appendChild(eNhom);
			}
		}
		return docToString(doc);
	}
	public static String layTinNhan(ArrayList<TinNhanPOJO> lstTinNhan){
		Document doc=BuildXML("Message");
		Element root = (Element) doc.getFirstChild();
		if(lstTinNhan!=null){
			for(int i=0;i<lstTinNhan.size();i++){
				root.appendChild(lstTinNhan.get(i).createXMLElement(doc));
			}
		}
		return docToString(doc);
	}
	public static String layDanhSachCayThaoLuan(ArrayList<CayThaoLuanPOJO> lstCay){
		Document doc=BuildXML("DSCay");
		Element root=(Element)doc.getFirstChild();
		if(lstCay!=null){
			for(CayThaoLuanPOJO cay:lstCay){
				root.appendChild(cay.createXMLElement(doc));
			}
		}
		return docToString(doc);
	}
	public static void taoNodeCay(Element element, Document doc,ArrayList<NodeThaoLuanPOJO> lstNode, int idNodeCha){
		for(int i=0;i<lstNode.size();i++){
			if(lstNode.get(i).getIdNodeCha()==idNodeCha){
				Element e=lstNode.get(i).createXMLElement(doc);	
				element.appendChild(e);
				taoNodeCay(e, doc, lstNode, lstNode.get(i).getId());
				
			}
		}
	}
	public static String taoCay(CayThaoLuanPOJO cay){
		Document doc=BuildXML("Root");
		
		NodeThaoLuanPOJO node=new NodeThaoLuanPOJO();
		node.setGhiChu("");
		node.setId(0);
		node.setIdCay(cay.getId());
		node.setIdNguoiTao(cay.getIdNguoiTao());
		node.setIdNodeCha(0);
		node.setNoiDung(cay.getNoiDung());
		node.setVersion(cay.getVersion());
		Element element=node.createXMLElement(doc);
		
		ArrayList<NodeThaoLuanPOJO> lstNode=new NodeThaoLuanDA().getByIDCay(cay.getId());
		
		taoNodeCay(element, doc, lstNode, 0);
		doc.getFirstChild().appendChild(element);
		String rs= docToString(doc);
		System.out.println("Tao cay"+rs);
		return rs;
	}
	public static String nodeToXMLString(NodeThaoLuanPOJO node){
		Document doc=BuildXML("Root");
		doc.getFirstChild().appendChild(node.createXMLElement(doc));
		return docToString(doc);
	}
	public static String docToString(Document doc){
		// Generate text từ doc
				OutputFormat format = new OutputFormat(doc);
				StringWriter stringOut = new StringWriter();
				XMLSerializer serial = new XMLSerializer(stringOut, format);
				try {
					serial.serialize(doc);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return stringOut.toString();
	}
}
